package com.yscp.ysdemo.filter;


import com.yscp.ysdemo.common.WordsFilter;
import com.yscp.ysdemo.common.sensitiveWordsRequestWrapper;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

//流程依旧是reqWrapper -> reqWrapper -> ......reqWrapper ->requset，主要是在reqWrapper一定要先调用父类的对应方法再增强
//其实就是层层包装，每次流经过滤器都会new一个增强的reqWrapper，这个reqWrapper在初始化用的是之前的reqWrapper，见new sensitiveWordsRequestWrapper((HttpServletRequest)servletRequest)
//之前的req已经被包装成其他reqWrapper，这时候相当于在上一个reqWrapper再包装一层，层层增强。使用这种包装的方法来过滤主要还是因为
//没有提供修改request参数数据的方法，没有对应的set方法。
public class sensitiveWordsFilter implements Filter {
    private WordsFilter wordsFilter = new WordsFilter();
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        filterChain.doFilter(new sensitiveWordsRequestWrapper((HttpServletRequest)servletRequest),servletResponse);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
